<template>
  <div>
    <el-date-picker class="formDate" v-if="mode === 'D'" :disabled="true" type="datetimerange"
      value-format="YYYY-MM-DD HH:mm" format="YYYY-MM-DD HH:mm" :start-placeholder="!form ? '' : form.startPlaceholder"
      :end-placeholder="!form ? '' : form.endPlaceholder" />
    <el-date-picker v-else class="formDate" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss"
      :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]" type="datetimerange"
      v-model="form.props.value" :disabled="form.perm === 'R'" :start-placeholder="form.startPlaceholder"
      :end-placeholder="form.endPlaceholder" />
  </div>
</template>

<script lang="ts" setup>
import { defineExpose, watch } from 'vue';
import { useFlowStore } from '/@/views/flow/workflow/stores/flow';
import axios from "axios";
import {getUserInfo} from '/@/api/login/index'
import {params} from "vite-plugin-top-level-await";
import {data} from "autoprefixer";
let store = useFlowStore();
let props = defineProps({
  mode: {
    type: String,
    default: 'D',
  },

  form: {
    type: Object,
    default: () => { },
  },
});
onMounted(() => {
  getUser()
})
const username = ref("")
const userid = ref("")
const getUser = async () => {
  const res = await getUserInfo()
  console.log(res)
  username.value = res.data.sysUser.name
  userid.value = res.data.sysUser.id
}

console.log(props.form)
function calculateAndRoundUpDaysDifference(startDateStr: string, endDateStr: string): number {
  // 将字符串转换为Date对象
  const startDate = new Date(startDateStr);
  const endDate = new Date(endDateStr);

  // 确保日期有效
  if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
    throw new Error('Invalid date string');
  }

  // 计算时间差（以毫秒为单位）
  const timeDifferenceMs = endDate - startDate;

  // 将时间差转换为天数
  const timeDifferenceDays = timeDifferenceMs / (1000 * 60 * 60 * 24);

  // 以0.5天为单位向上取整
  const roundedDaysDifference = Math.ceil(timeDifferenceDays / 0.5) * 0.5;

  // 返回结果（以天为单位）
  return roundedDaysDifference;
}
watch(props.form, (newVal, oldVal) => {
  console.log(props.form)
  if (newVal.props.value && newVal.props.value.length == 2) {
    store.setLeaveNumber(calculateAndRoundUpDaysDifference(newVal.props.value[0], newVal.props.value[1]));
  }

  axios.get(`http://10.2.1.130:9999/personnel/sign/ischeck?certificateNo=${userid.value}&personName=${username.value}&startTime=${props.form.props.value[0]}&endTime=${props.form.props.value[1]}`).then((res)=> {
    if(res.data != null) {
      store.setDKdata(1)
      console.log(1111)
    }
    store.setDKdata(0)
    console.log(2222)
  }).catch(() => {
    console.log(111333222)
  })
}, {
  deep: true,
})
const { proxy } = getCurrentInstance();
const getValidateRule = () => {
  var item = props.form;
  var itemProps = item.props;

  var checkConfig = (rule: any, value: any, callback: any) => {
    if (item.required) {
      if (!value) {
        return callback(new Error('请填写' + item.name));
      }
    }
    if (!value) {
      return callback();
    }

    if (itemProps.minLength) {
      if (value.length < itemProps.minLength) {
        return callback(new Error('长度不能小于' + itemProps.minLength));
      }
    }

    if (itemProps.maxLength) {
      if (value.length > itemProps.maxLength) {
        return callback(new Error('长度不能大于' + itemProps.maxLength));
      }
    }
    if (itemProps.regex) {
      let regExp = new RegExp(itemProps.regex);
      if (!regExp.test(value)) {
        return callback(new Error(itemProps.regexDesc));
      }
    }
    return callback();
  };
  let ruleArray = [
    {
      validator: checkConfig,
      trigger: 'blur',
    },
  ];
  if (item.required) {
    ruleArray.push({ required: true, message: '请填写' + item.name, trigger: 'blur' });
  }
  return ruleArray;
};
defineExpose({ getValidateRule });
</script>

<style scoped>
:deep(.formDate div.el-input__wrapper) {
  width: 100% !important;
}

:deep(.formDate) {
  width: 100% !important;
}
</style>
